package br.mikhas.task;

import static br.mikhas.util.Assert.*;

import java.util.Date;

import br.mikhas.task.metadata.User;

/**
 * Register an event which changed a task state
 * 
 * @author Mikhail Domanoski
 * 
 */
public class History {
	/**
	 * Task which have been affected
	 */
	protected Task task;
	/**
	 * The user which fired the event
	 */
	protected User user;
	/**
	 * The time the event has occured
	 */
	protected Date when = new Date();
	/**
	 * The event description
	 */
	protected String description;

	/**
	 * @return the task
	 */
	public Task getTask() {
		return task;
	}

	/**
	 * @param task
	 *            the task to set
	 */
	public void setTask(Task task) {
		notNull(task);
		this.task = task;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(User user) {
		notNull(user);
		this.user = user;
	}

	/**
	 * @return the when
	 */
	public Date getWhen() {
		return when;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		notEmpty(description);
		this.description = description;
	}

	/**
	 * Creates a status change event history
	 * 
	 * @param user
	 *            The user who chnaged the status
	 * @param task
	 *            The task which has chnaged
	 * @return The envent history
	 */
	public static History statusChange(User user, Task task) {
		notNull(task);
		notNull(user);
		History history = new History();
		history.task = task;
		history.user = user;
		history.description = "Task status changed to " + task.getStatus();

		task.addHistoryEntry(history);

		return history;
	}

	public static History occurrenceCreation(Task task) {
		notNull(task);
		History history = new History();
		history.task = task;
		history.user = null;
		history.description = "Create new task occurrence";

		task.addHistoryEntry(history);

		return history;
	}

}
